﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmDmngoaite.
	/// </summary>
	public class frmDmngoaite : System.Windows.Forms.Form
	{
		#region khai bao control

		private System.Windows.Forms.DataGrid dgdmngte;
		private System.Windows.Forms.Label lblmangte;
		private System.Windows.Forms.Label lbltenngte;
		private System.Windows.Forms.Label lbltygia;
		private System.Windows.Forms.TextBox txtmangte;
		private System.Windows.Forms.TextBox txttenngte;
		private System.Windows.Forms.TextBox txttygia;
		#endregion
		#region khai bao bien su dung
		private string sql="";
		private string s_user="",s_mmyy="";
		private int i_userid=-1,i_stt;
		long l_id=-1;
		AccessData a=new AccessData();
		DataSet ds_data1=null,ds_data2=null,dsngte=null;

		#endregion
		private System.Windows.Forms.TextBox txtfind;
		private System.Windows.Forms.CheckBox chsd;
		private System.Windows.Forms.Button btnthem;
		private System.Windows.Forms.Button btnsua;
		private System.Windows.Forms.Button btnluu;
		private System.Windows.Forms.Button btnboqua;
		private System.Windows.Forms.Button btnhuy;
		private System.Windows.Forms.Button btnin;
		private System.Windows.Forms.Button btnketthuc;
        private GroupBox groupBox1;


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDmngoaite(AccessData acc,string mmyy,int iduser)
		{

			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;s_mmyy=mmyy;i_userid=iduser;

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDmngoaite));
            this.dgdmngte = new System.Windows.Forms.DataGrid();
            this.lblmangte = new System.Windows.Forms.Label();
            this.lbltenngte = new System.Windows.Forms.Label();
            this.lbltygia = new System.Windows.Forms.Label();
            this.txtmangte = new System.Windows.Forms.TextBox();
            this.txttenngte = new System.Windows.Forms.TextBox();
            this.txttygia = new System.Windows.Forms.TextBox();
            this.txtfind = new System.Windows.Forms.TextBox();
            this.chsd = new System.Windows.Forms.CheckBox();
            this.btnthem = new System.Windows.Forms.Button();
            this.btnsua = new System.Windows.Forms.Button();
            this.btnluu = new System.Windows.Forms.Button();
            this.btnboqua = new System.Windows.Forms.Button();
            this.btnhuy = new System.Windows.Forms.Button();
            this.btnin = new System.Windows.Forms.Button();
            this.btnketthuc = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgdmngte)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // dgdmngte
            // 
            this.dgdmngte.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dgdmngte.BackColor = System.Drawing.SystemColors.Highlight;
            this.dgdmngte.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgdmngte.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dgdmngte.DataMember = "";
            this.dgdmngte.FlatMode = true;
            this.dgdmngte.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgdmngte.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dgdmngte.Location = new System.Drawing.Point(0, 0);
            this.dgdmngte.Name = "dgdmngte";
            this.dgdmngte.RowHeadersVisible = false;
            this.dgdmngte.Size = new System.Drawing.Size(864, 224);
            this.dgdmngte.TabIndex = 1;
            this.dgdmngte.CurrentCellChanged += new System.EventHandler(this.dgdmngte_CurrentCellChanged);
            // 
            // lblmangte
            // 
            this.lblmangte.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lblmangte.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblmangte.Location = new System.Drawing.Point(6, 16);
            this.lblmangte.Name = "lblmangte";
            this.lblmangte.Size = new System.Drawing.Size(26, 23);
            this.lblmangte.TabIndex = 9;
            this.lblmangte.Text = "Mã ";
            // 
            // lbltenngte
            // 
            this.lbltenngte.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lbltenngte.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbltenngte.Location = new System.Drawing.Point(150, 18);
            this.lbltenngte.Name = "lbltenngte";
            this.lbltenngte.Size = new System.Drawing.Size(72, 23);
            this.lbltenngte.TabIndex = 10;
            this.lbltenngte.Text = "Tên ngoại tệ";
            // 
            // lbltygia
            // 
            this.lbltygia.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lbltygia.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbltygia.Location = new System.Drawing.Point(530, 16);
            this.lbltygia.Name = "lbltygia";
            this.lbltygia.Size = new System.Drawing.Size(40, 23);
            this.lbltygia.TabIndex = 11;
            this.lbltygia.Text = "Tỷ giá";
            // 
            // txtmangte
            // 
            this.txtmangte.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.txtmangte.Location = new System.Drawing.Point(38, 13);
            this.txtmangte.Name = "txtmangte";
            this.txtmangte.Size = new System.Drawing.Size(100, 21);
            this.txtmangte.TabIndex = 1;
            this.txtmangte.Validated += new System.EventHandler(this.txtmangte_Validated);
            this.txtmangte.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtmangte_KeyDown);
            // 
            // txttenngte
            // 
            this.txttenngte.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.txttenngte.Location = new System.Drawing.Point(228, 13);
            this.txttenngte.Name = "txttenngte";
            this.txttenngte.Size = new System.Drawing.Size(281, 21);
            this.txttenngte.TabIndex = 2;
            this.txttenngte.Validated += new System.EventHandler(this.txttenngte_Validated);
            this.txttenngte.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtmangte_KeyDown);
            // 
            // txttygia
            // 
            this.txttygia.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.txttygia.Location = new System.Drawing.Point(576, 13);
            this.txttygia.Name = "txttygia";
            this.txttygia.Size = new System.Drawing.Size(152, 21);
            this.txttygia.TabIndex = 3;
            this.txttygia.Validated += new System.EventHandler(this.txttygia_Validated);
            this.txttygia.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtmangte_KeyDown);
            // 
            // txtfind
            // 
            this.txtfind.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtfind.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtfind.ForeColor = System.Drawing.Color.Red;
            this.txtfind.Location = new System.Drawing.Point(0, 0);
            this.txtfind.Name = "txtfind";
            this.txtfind.Size = new System.Drawing.Size(864, 21);
            this.txtfind.TabIndex = 30;
            this.txtfind.Text = "Tìm kiếm";
            this.txtfind.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtfind.TextChanged += new System.EventHandler(this.txtfind_TextChanged);
            this.txtfind.Enter += new System.EventHandler(this.txtfind_Enter);
            this.txtfind.TextAlignChanged += new System.EventHandler(this.txtfind_TextAlignChanged);
            // 
            // chsd
            // 
            this.chsd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.chsd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chsd.Location = new System.Drawing.Point(754, 13);
            this.chsd.Name = "chsd";
            this.chsd.Size = new System.Drawing.Size(104, 24);
            this.chsd.TabIndex = 4;
            this.chsd.Text = "Không sử dụng ";
            this.chsd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtmangte_KeyDown);
            // 
            // btnthem
            // 
            this.btnthem.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnthem.Enabled = false;
            this.btnthem.Image = ((System.Drawing.Image)(resources.GetObject("btnthem.Image")));
            this.btnthem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnthem.Location = new System.Drawing.Point(247, 282);
            this.btnthem.Name = "btnthem";
            this.btnthem.Size = new System.Drawing.Size(60, 25);
            this.btnthem.TabIndex = 81;
            this.btnthem.Text = "&Thêm";
            this.btnthem.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btnthem.Click += new System.EventHandler(this.btnthem_Click);
            // 
            // btnsua
            // 
            this.btnsua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnsua.Image = ((System.Drawing.Image)(resources.GetObject("btnsua.Image")));
            this.btnsua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnsua.Location = new System.Drawing.Point(311, 282);
            this.btnsua.Name = "btnsua";
            this.btnsua.Size = new System.Drawing.Size(60, 25);
            this.btnsua.TabIndex = 86;
            this.btnsua.Text = "     &Sửa";
            this.btnsua.Click += new System.EventHandler(this.btnsua_Click);
            // 
            // btnluu
            // 
            this.btnluu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnluu.Enabled = false;
            this.btnluu.Image = ((System.Drawing.Image)(resources.GetObject("btnluu.Image")));
            this.btnluu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnluu.Location = new System.Drawing.Point(375, 282);
            this.btnluu.Name = "btnluu";
            this.btnluu.Size = new System.Drawing.Size(60, 25);
            this.btnluu.TabIndex = 87;
            this.btnluu.Text = "     &Lưu";
            this.btnluu.Click += new System.EventHandler(this.btnluu_Click);
            // 
            // btnboqua
            // 
            this.btnboqua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnboqua.Enabled = false;
            this.btnboqua.Image = ((System.Drawing.Image)(resources.GetObject("btnboqua.Image")));
            this.btnboqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnboqua.Location = new System.Drawing.Point(439, 282);
            this.btnboqua.Name = "btnboqua";
            this.btnboqua.Size = new System.Drawing.Size(60, 25);
            this.btnboqua.TabIndex = 88;
            this.btnboqua.Text = "&Bỏ qua";
            this.btnboqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btnboqua.Click += new System.EventHandler(this.btnboqua_Click);
            // 
            // btnhuy
            // 
            this.btnhuy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnhuy.Image = ((System.Drawing.Image)(resources.GetObject("btnhuy.Image")));
            this.btnhuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnhuy.Location = new System.Drawing.Point(503, 282);
            this.btnhuy.Name = "btnhuy";
            this.btnhuy.Size = new System.Drawing.Size(60, 25);
            this.btnhuy.TabIndex = 89;
            this.btnhuy.Text = "     &Hủy";
            this.btnhuy.Click += new System.EventHandler(this.btnhuy_Click);
            // 
            // btnin
            // 
            this.btnin.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnin.Image = ((System.Drawing.Image)(resources.GetObject("btnin.Image")));
            this.btnin.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnin.Location = new System.Drawing.Point(567, 282);
            this.btnin.Name = "btnin";
            this.btnin.Size = new System.Drawing.Size(60, 25);
            this.btnin.TabIndex = 90;
            this.btnin.Text = "      &In";
            this.btnin.Click += new System.EventHandler(this.btnin_Click);
            // 
            // btnketthuc
            // 
            this.btnketthuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnketthuc.Image = ((System.Drawing.Image)(resources.GetObject("btnketthuc.Image")));
            this.btnketthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnketthuc.Location = new System.Drawing.Point(631, 282);
            this.btnketthuc.Name = "btnketthuc";
            this.btnketthuc.Size = new System.Drawing.Size(70, 25);
            this.btnketthuc.TabIndex = 91;
            this.btnketthuc.Text = "&Kết thúc";
            this.btnketthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btnketthuc.Click += new System.EventHandler(this.btnketthuc_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.lbltenngte);
            this.groupBox1.Controls.Add(this.lblmangte);
            this.groupBox1.Controls.Add(this.txtmangte);
            this.groupBox1.Controls.Add(this.txttenngte);
            this.groupBox1.Controls.Add(this.lbltygia);
            this.groupBox1.Controls.Add(this.txttygia);
            this.groupBox1.Controls.Add(this.chsd);
            this.groupBox1.Location = new System.Drawing.Point(0, 232);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(864, 44);
            this.groupBox1.TabIndex = 92;
            this.groupBox1.TabStop = false;
            // 
            // frmDmngoaite
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(864, 310);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.btnketthuc);
            this.Controls.Add(this.btnin);
            this.Controls.Add(this.btnhuy);
            this.Controls.Add(this.btnboqua);
            this.Controls.Add(this.btnluu);
            this.Controls.Add(this.btnsua);
            this.Controls.Add(this.btnthem);
            this.Controls.Add(this.txtfind);
            this.Controls.Add(this.dgdmngte);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmDmngoaite";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục ngoại tệ";
            this.Load += new System.EventHandler(this.frmDmngoaite_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgdmngte)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void load_datangoaite()
		{
			sql="select id, ma,ten,tygia,to_char(ngay,'dd/mm/yyyy') as ngay, sudung from "+s_user+".a_dmngoaite order by id";
			dsngte=a.get_data(sql).Copy();
			dgdmngte.DataSource=dsngte.Tables[0];
			
		}
		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dsngte.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();			
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dgdmngte.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();		
			TextCol.MappingName = "ma";
			TextCol.HeaderText = "Mã";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dgdmngte.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ten";
			TextCol.HeaderText = "Tên";
			TextCol.Width = 200;
			ts.GridColumnStyles.Add(TextCol);
			dgdmngte.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ngay";
			TextCol.HeaderText = "Ngày";
			TextCol.Width = 100;
			ts.GridColumnStyles.Add(TextCol);
			dgdmngte.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tygia";
			TextCol.HeaderText = "Tỷ giá";
			TextCol.Width = dgdmngte.Width-80-200-100;
			ts.GridColumnStyles.Add(TextCol);
			dgdmngte.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sudung";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dgdmngte.TableStyles.Add(ts);
			
		}
		private void ena_object(bool ena)
		{
			dgdmngte.Enabled=!ena;
			txtmangte.Enabled=ena;
			txttenngte.Enabled=ena;
			txttygia.Enabled=ena;
			btnthem.Enabled=!ena;
			btnsua.Enabled=!ena;
			btnluu.Enabled=ena;
			btnin.Enabled=!ena;
			btnboqua.Enabled=ena;
			btnhuy.Enabled=!ena;
			btnketthuc.Enabled=!ena;
			chsd.Enabled=ena;
		}
		private void ref_text()
		{
			try
			{
				int i=dgdmngte.CurrentCell.RowNumber;				
				txtmangte.Text=dgdmngte[i,1].ToString();
				txttenngte.Text=dgdmngte[i,2].ToString().Trim();				
				txttygia.Text=dgdmngte[i,4].ToString().Trim();
				chsd.Checked=dgdmngte[i,5].ToString().Trim()=="0";
		}
			catch{}
		}

		private void frmDmngoaite_Load(object sender, System.EventArgs e)
		{
			s_user=a.user;
			load_datangoaite();
			AddGridTableStyle();
			ena_object(false);
		}

		private void btnketthuc_Click(object sender, System.EventArgs e)
		{
			a.close();System.GC.Collect();this.Close();
		}

		private void btnthem_Click(object sender, System.EventArgs e)
		{
			txtmangte.Text=txttenngte.Text=txttygia.Text="";
			ena_object(true);
			l_id=0;
			txtmangte.Focus();
			
		}

		private void btnboqua_Click(object sender, System.EventArgs e)
		{
			ref_text();
			ena_object(false);
		}

		private void btnluu_Click(object sender, System.EventArgs e)
		{
			if(kiemtra())
			{
				if (l_id==0)
				{
					try
					{
						l_id=long.Parse(a.get_data("select max(id) from "+s_user+".a_dmngoaite").Tables[0].Rows[0][0].ToString())+1;
						i_stt=int.Parse(a.get_data("select nvl(max(stt),0) from "+s_user+".a_dmngoaite").Tables[0].Rows[0][0].ToString())+1;
					}
					catch{l_id=1;}
				}
				if (!a.upd_dmngoaite(l_id,txtmangte.Text,txttenngte.Text,s_mmyy,decimal.Parse(txttygia.Text),(chsd.Checked)?0:1,i_userid))
				{
					MessageBox.Show("Không c?p nh?t thông tin "+this.Text.Trim()+" !",a.Msg);
					return;
				}
				load_datangoaite();
				ref_text();
				ena_object(false);
			}
			else
				return;
		
		}
		private bool kiemtra()
		{
			if(txtmangte.Text=="")
			{
				MessageBox.Show("B?n chua nh?p mã ngo?i t? ",a.Msg);
				txtmangte.Focus();
				return false;
			}
			if(txttenngte.Text=="")
			{
				MessageBox.Show("B?n chua nh?p tên ngo?i t?",a.Msg);
				txttenngte.Focus();
				return false;
			}
			if(txttygia.Text=="")
			{
				MessageBox.Show("B?n chua nh?p t? giá c?a ngo?i t?",a.Msg);
				txttygia.Focus();
				return false;
			}
			return true;
		}

		private void txttygia_Validated(object sender, System.EventArgs e)
		{
			try
			{
				decimal st=(txttygia.Text!="")?decimal.Parse(txttygia.Text):0;
				txttygia.Text=st.ToString("###,###,###,###,###");
			}
			catch{}
		}

		private void txtmangte_Validated(object sender, System.EventArgs e)
		{
			if (l_id==-1 && txtmangte.Text!="")
			{
				DataRow r1=a.getrowbyid(dsngte.Tables[0],"ma='"+txtmangte.Text+"'");
				if (r1!=null)
				{
					MessageBox.Show("Mã s? dã nh?p!",a.Msg);
					txtmangte.Focus();
				}
			}
		}

		private void txttenngte_Validated(object sender, System.EventArgs e)
		{
			if (l_id==-1 && txttenngte.Text!="")
			{
				DataRow r1=a.getrowbyid(dsngte.Tables[0],"ten='"+txttenngte.Text+"'");
				if (r1!=null)
				{
					MessageBox.Show("Mã s? dã nh?p !",a.Msg);
					txttenngte.Focus();
				}
			}
		}

		private void btnsua_Click(object sender, System.EventArgs e)
		{
			if (dsngte.Tables[0].Rows.Count==0) return;
			l_id=long.Parse(dgdmngte[dgdmngte.CurrentCell.RowNumber,0].ToString());
			ena_object(true);
			txtmangte.Focus();
		}

		private void dgdmngte_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text();
		}

		private void btnhuy_Click(object sender, System.EventArgs e)
		{
			if (dsngte.Tables[0].Rows.Count==1)
			{
				MessageBox.Show("Không cho phép h?y ?",a.Msg);
				return;
			}	
			try
			{
				string mmyy=s_mmyy.Substring(8,2);
				string si="select * from "+s_user+".a_ctct"+s_mmyy.Substring(8,2)+" where ngoaite="+long.Parse(dgdmngte[dgdmngte.CurrentCell.RowNumber,0].ToString());
				string se="select * from "+s_user+".a_so"+s_mmyy.Substring(8,2)+" where ngoaite="+long.Parse(dgdmngte[dgdmngte.CurrentCell.RowNumber,0].ToString());
				ds_data1=a.get_data("select * from "+s_user+".a_ctct"+s_mmyy.Substring(8,2)+" where ngoaite="+long.Parse(dgdmngte[dgdmngte.CurrentCell.RowNumber,0].ToString()));
				ds_data2=a.get_data("select * from "+s_user+".a_so"+s_mmyy.Substring(8,2)+" where ngoaite="+long.Parse(dgdmngte[dgdmngte.CurrentCell.RowNumber,0].ToString()));
				if (ds_data1.Tables[0].Rows.Count!=0 ||ds_data2.Tables[0].Rows.Count!=0)
				{
					MessageBox.Show("N?i dung dang s? d?ng không cho phép h?y !",a.Msg);
					return;
				}
			}
			catch{}
			if (MessageBox.Show("Ð?ng ý h?y thông tin này !",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				a.execute_data("delete from "+s_user+".a_dmngoaite where id="+long.Parse(dgdmngte[dgdmngte.CurrentCell.RowNumber,0].ToString()));
				load_datangoaite ();
				ref_text();
			}
		}

		private void txtmangte_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter||e.KeyCode==Keys.Tab)
				SendKeys.Send("{tab}");
		}
		private void refeshchildren(string find)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[dgdmngte.DataSource];
				DataView dv=(DataView)cm.List;
				sql="ma like '%"+find.Trim()+"%' or ten like '%"+find.Trim()+"%' or tygia like '%"+find.Trim()+"%'";
				dv.RowFilter=sql;
				ref_text();
			}
			catch{}
		}

		private void txtfind_TextAlignChanged(object sender, System.EventArgs e)
		{
			if(this.ActiveControl==txtfind) refeshchildren(txtfind.Text);		
		}

		private void txtfind_Enter(object sender, System.EventArgs e)
		{
			txtfind.Text="";
		}

		private void txtfind_TextChanged(object sender, System.EventArgs e)
		{
			if(this.ActiveControl==txtfind) refeshchildren(txtfind.Text);
		}

		private void btnin_Click(object sender, System.EventArgs e)
		{
            //sql="select ma,ten,tygia,to_char(ngay,'dd/mm/yyyy') from "+a.user+".a_dmngoaite";
            //dsngte=null;
            //dsngte=a.get_data(sql);
            //dsngte.WriteXml("..\\xml\\KT_dmngoaite.xml",XmlWriteMode.WriteSchema);
            //frmReport_dm f=new frmReport_dm(a,dsngte,"KT_dmngoaite.rpt");
            //f.Show();
		}

	}
}
